import 'dart:io';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

showAlert(BuildContext context, String say) {
//    showAboutDialog(context: context, children: <Widget>[Text('sa')]);
  /*showGeneralDialog(context: context, pageBuilder: (context, an1, an2){
      return Text(say);
    });*/

  showDialog(
      context: context,
      builder: (context) => Center(
            child: Container(
              decoration: BoxDecoration(
                  color: Colors.white, borderRadius: BorderRadius.circular(8)),
              padding: const EdgeInsets.all(24.0),
              margin: const EdgeInsets.all(24.0),
              child: Text(
                say,
                style: TextStyle(
                    fontSize: 15,
                    color: Colors.blueGrey,
                    decoration: TextDecoration.none),
              ),
            ),
          ),
      barrierDismissible: true);
}

Future<int> showMyBottomSheet(BuildContext context, List<String> data) async {
  final result = await showModalBottomSheet(
      context: context,
      builder: (context) {
        return BottomSheet(
            onClosing: () {},
            builder: (context) {
              return ListView.separated(
                itemBuilder: (context, po) {
                  return ListTile(
                    title: Text(data[po]),
                    onTap: () => Navigator.pop(context, po),
                  );
                },
                separatorBuilder: (context, po) => Divider(),
                itemCount: data.length,
              );
            });
      });

  return result as int? ?? -1;
}

showMyBottomSheet2(BuildContext context, Map<String, Function()> data) {
  if (data.length < 1) return;

  final keys = data.keys.toList();
  showMyBottomSheet(context, keys).then((value) {
    if (value < 0) return;
    data[keys[value]]!();
  });
}

Future<SharedPreferences> getSharedPreferences() async {
  return SharedPreferences.getInstance();
}

showToast(String text) => Fluttertoast.showToast(msg: text);

pushMaterial(BuildContext context, Widget page) {
  Navigator.push(context, MaterialPageRoute(builder: (context) => page));
}

Future<File> getFile(String subPathName, {bool isExternal = false}) async {
  final directory = isExternal
      ? (await getExternalStorageDirectory())
      : (await getApplicationDocumentsDirectory());
  final file = File('${directory!.path}/subPathName');

  return file;
}
